﻿@using Aspire.Dashboard.Components.Controls.Grid
@using Aspire.Dashboard.Model
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Utils
@inject IStringLocalizer<ControlsStrings> ControlStringsLoc

<div class="resource-details-layout">

    <FluentToolbar Orientation="Orientation.Horizontal">
        <FluentAnchor Appearance="Appearance.Lightweight" Href="@DashboardUrls.ConsoleLogsUrl(Resource?.Name)" slot="end">@Loc[Resources.ResourceDetailsViewConsoleLogs]</FluentAnchor>

        @if (ShowSpecOnlyToggle)
        {
            <FluentIconSwitch Appearance="Appearance.Lightweight"
                              Disabled="IsSpecOnlyToggleDisabled"
                              CheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesFilterToggleShowSpecOnly)]"
                              UncheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesFilterToggleShowAll)]"
                              OnToggle="@(() => _showAll = !_showAll)"
                              CheckedIcon="@(new Icons.Regular.Size16.DocumentHeader())"
                              UncheckedIcon="@(new Icons.Regular.Size16.DocumentOnePage())"
                              slot="end"/>
        }

        <FluentIconSwitch @bind-Value="@_areEnvironmentVariablesMasked"
                          Appearance="Appearance.Lightweight"
                          CheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesShowVariableValues)]"
                          UncheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesHideVariableValues)]"
                          OnToggle="@ToggleMaskState"
                          CheckedIcon="@(new Icons.Regular.Size16.Eye())"
                          UncheckedIcon="@(new Icons.Regular.Size16.EyeOff())"
                          slot="end" />

        <FluentSearch Placeholder="@ControlStringsLoc[nameof(ControlsStrings.FilterPlaceholder)]"
                      Immediate="true"
                      Autofocus="true"
                      @bind-Value="_filter"
                      slot="end" />
    </FluentToolbar>
    <div class="property-grid-container">
        <FluentAccordion>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsResourceHeader)]" Expanded="true">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredResourceValues.Count()
                    </FluentBadge>
                </div>
                <FluentDataGrid ResizeLabel="@AspireFluentDataGridHeaderCell.GetResizeLabel(ControlStringsLoc)"
                                ResizeType="DataGridResizeType.Discrete"
                                Items="@FilteredResourceValues"
                                ResizableColumns="true"
                                Style="width:100%"
                                GenerateHeader="GenerateHeaderOption.Sticky"
                                GridTemplateColumns="1fr 1.5fr"
                                ShowHover="true">
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]" Class="nameColumn">
                        <GridValue
                            Value="@(context.KnownProperty?.DisplayName ?? context.Key)"
                            ValueDescription="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]"
                            ToolTip="@context.Key" />
                    </AspireTemplateColumn>
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.PropertyGridValueColumnHeader)]" Class="valueColumn">
                        <GridValue
                            Value="@GetDisplayedValue(TimeProvider, context)"
                            ValueDescription="@(context.KnownProperty?.DisplayName ?? context.Key)"
                            ToolTip="@context.Tooltip" />
                    </AspireTemplateColumn>
                </FluentDataGrid>
            </FluentAccordionItem>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsEndpointsHeader)]" Expanded="true">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredEndpoints.Count()
                    </FluentBadge>
                </div>
                <FluentDataGrid ResizeLabel="@AspireFluentDataGridHeaderCell.GetResizeLabel(ControlStringsLoc)"
                                ResizeType="DataGridResizeType.Discrete"
                                Items="@FilteredEndpoints"
                                ResizableColumns="true"
                                Style="width:100%"
                                GenerateHeader="GenerateHeaderOption.Sticky"
                                GridTemplateColumns="1fr 1.5fr"
                                ShowHover="true">
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]" Class="nameColumn">
                        <GridValue Value="@context.Name" ValueDescription="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]" />
                    </AspireTemplateColumn>
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.PropertyGridValueColumnHeader)]" Class="valueColumn">
                        <GridValue
                            Value="@context.Text"
                            ValueDescription="@context.Name"
                            MaxDisplayLength="0">
                            <ContentAfterValue>
                                @if (context.Url != null)
                                {
                                    <a href="@context.Url" target="_blank">@context.Url</a>
                                }
                                else
                                {
                                    @context.Text
                                }
                            </ContentAfterValue>
                        </GridValue>
                    </AspireTemplateColumn>
                </FluentDataGrid>
            </FluentAccordionItem>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsEnvironmentVariablesHeader)]" Expanded="true">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredItems.Count()
                    </FluentBadge>
                </div>
                <PropertyGrid TItem="EnvironmentVariableViewModel"
                              Items="@FilteredItems"
                              NameColumnValue="(vm) => vm.Name"
                              ValueColumnValue="(vm) => vm.Value"
                              NameSort="_nameSort"
                              ValueSort="_valueSort"
                              EnableValueMasking="true"
                              GetIsItemMasked="(vm) => vm.IsValueMasked"
                              SetIsItemMasked="(vm, newValue) => vm.IsValueMasked = newValue"
                              IsMaskedChanged="@CheckAllMaskStates"
                              HighlightText="@_filter"
                              GridTemplateColumns="1fr 1.5fr" />
            </FluentAccordionItem>
            @if (Resource.IsContainer())
            {
                <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsVolumesHeader)]" Expanded="true">
                    <div slot="end">
                        <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                            @FilteredVolumes.Count()
                        </FluentBadge>
                    </div>
                    <PropertyGrid TItem="VolumeViewModel"
                                  Items="@FilteredVolumes"
                                  NameColumnValue="(vm) => vm.Source"
                                  ValueColumnValue="(vm) => vm.Target"
                                  IsNameSortable="false"
                                  IsValueSortable="false"
                                  EnableValueMasking="false"
                                  HighlightText="@_filter"
                                  GridTemplateColumns="1fr 1.5fr">
                        <ExtraValueContent>
                            <div>
                                @if (context.IsReadOnly)
                                {
                                    <FluentIcon Value="@(new Icons.Regular.Size16.LockClosed())" Style="vertical-align: text-bottom" />
                                }
                                @context.MountType
                            </div>
                        </ExtraValueContent>
                    </PropertyGrid>
                </FluentAccordionItem>
            }
        </FluentAccordion>
    </div>
</div>
